<?php
Prado::using('Application.common.common');
class ManageReviews extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Manage Reviews";
			$this->AdminMenu->lnkManageFoodPlace->CssClass = "active";
			
			$this->txtFoodPlaceID->Value = ($this->Request->contains('fid')) ? TPropertyValue::ensureInteger($this->Request['fid']) : 0;	
			$foodPlaceRecord = FoodPlaceRecord::finder()->findByPk($this->txtFoodPlaceID->Value);
			$this->lblFoodPlaceName->Text = $foodPlaceRecord->Name;
			$this->populateReviews();	
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'ur')
					$this->Notice->Text = $this->Application->getModule('message')->translate('UPDATE_SUCCESS','Review','');
				else if ($this->Request['m'] == 'dr')
					$this->Notice->Text = $this->Application->getModule('message')->translate('DELETE_SUCCESS','Review','');	
			}
		}
	}
	
	protected function populateReviews()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "food_place_id = " . $this->txtFoodPlaceID->Value;				
		$criteria->OrdersBy["c_date"] = "desc";							
		$activeRecord = Prado::createComponent("ReviewRecord");
		$limit = null;
		$offset = null;
		$criteria->Limit = $limit;
		$criteria->Offset = $offset;
		$items = $activeRecord->finder()->findAll($criteria);
		$this->ItemList_Request->DataSource = $items;
		$this->ItemList_Request->dataBind();
		$this->lblTotal->Text=" - Reviews (" . count($items) . ")";		
		if (count($items) <= 0)
		{
			$this->lblTotal->Text=" - There is no review for this food place.";
			$this->lblTotal->Style="text-transform:lower;";
		}
	}
	
	protected function list_ItemCreated($sender, $param)
	{
		if ($param->Item->ItemType == "Item" || $param->Item->ItemType == "AlternatingItem")
		{
			if ($param->Item->Data)
			{				
				$param->Item->colInfo->txtReview->Text=$param->Item->Data->Review;		
				
				if ($param->Item->Data->CreateBy == 0)
					$param->Item->colReviewer->lblReviewer->Text = "Guest";	
				else
				{
					$user = UserRecord::finder()->findByPk($param->Item->Data->CreateBy);
					$param->Item->colReviewer->lblReviewer->Text = $user->UserName;
				}
			}
		}
	}	
	
	protected function list_ItemCommand($sender, $param)
	{
		switch($param->CommandName)
		{
			case "save":		
				$reviewRecord = ReviewRecord::finder()->findByPk($param->Item->colButton->txtReviewID->Value);				
				$reviewRecord->Review = $param->Item->colInfo->txtReview->SafeText;				
				$reviewRecord->ModifyBy = $this->Application->User->ID;
				$reviewRecord->ModifyDate = time();							
				$reviewRecord->save();					
				$this->Response->redirect($this->Service->ConstructUrl('admin.ManageReviews', array('fid'=>$this->txtFoodPlaceID->Value, 'm'=>'ur')));			
				break;
			case "delete":		
				$criteria = new TActiveRecordCriteria;
				$criteria->Condition = "review_id = " .$param->Item->colButton->txtReviewID->Value;
				ReviewRecord::finder()->deleteAll($criteria);	
				$this->Response->redirect($this->Service->ConstructUrl('admin.ManageReviews', array('fid'=>$this->txtFoodPlaceID->Value, 'm'=>'dr')));			
				break;
			default:
				break;
		}

		$this->populatePhotos();	
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {
				$reviewRecord = new ReviewRecord;
				$reviewRecord->FoodPlaceID = $this->txtFoodPlaceID->Value;
				$reviewRecord->Review = $this->txtReview->SafeText;
				$reviewRecord->Status = 1;
				$reviewRecord->CreateBy = $reviewRecord->ModifyBy = 0; // 0: self created
				if ($this->Application->User->ID != "")
					$reviewRecord->CreateBy = $reviewRecord->ModifyBy = $this->Application->User->ID;
				$reviewRecord->CreateDate = $reviewRecord->ModifyDate = time();							
				$reviewRecord->save();	
				$this->Response->redirect($this->Service->ConstructUrl('admin.ManageReviews', array('fid'=>$this->txtFoodPlaceID->Value, 'm'=>'cr')));				
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("admin.ManageFoodPlaces"));				
	}
}

?>